<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Think\Page;

class PingqiangController extends AdminController {
    public function _initialize(){
        parent::_initialize();
    }
    
     /**
     * 会员列表
     */
    public function index(){
        $cat = M('Cat')->select();
        $this->assign('cat', $cat);
        $name = I('name') ? I('name') : null;
        $cat_id = I('cat_id') ? I('cat_id') : null;
        if (!empty($name)) {
            $where['name'] = array("like", "%" . $name . "%");
            $where['is_limit'] = 1;
        }else{
        	$where['is_limit'] = 1;
        }
        if (!empty($cat_id)) {
            $where['cat_id'] = $cat_id;
            $where['is_limit'] = 1;
        }else{
        	$where['is_limit'] = 1;
        }
        if(empty($where)){
        	$where['is_limit'] = 1;
        }
        $count = M('Goods')->where($where)->count();
        $Page = new Page($count, 8);
        setPageParameter($Page, array('name' => $name));
        $show = $Page->show();
        $list = M('Goods')->where($where)
                        ->limit($Page->firstRow . ',' . $Page->listRows)->order('goods_id desc')->select();
        $sumzlnum=0;
        $sumzlren=0;
        $sumnum=0;
        foreach($list as $k=>$v){
            $go=M('rob_goods_orders');
            $num=$go->where(array('goods_id'=>$v['goods_id']))->count();//订单拼抢次数
            $ox=$go->where(array('goods_id'=>$v['goods_id']))->select();
            $zlnum=0;
            $zlren=0;
            foreach($ox as $kk =>$vv){
            	$od=M('rob_orders');
            	$olist=$od->where(array('order_id'=>$vv['order_id']))->find();   	
            	if(olist){
            		$o_log=M('rob_log');
            		$zlrs=$o_log->where(array('goods_orders_id'=>$vv['order_id'],'open_id'=>$olist['open_id']))->count();
            		$zlcs=$o_log->where(array('rob_order_id'=>$vv['order_id']))->count();
            		$zlnum+=$zlcs;
            		$zlren=$zlrs; 
            	}	
            } 
            $list[$k]['zlnum']=$zlnum;
            $list[$k]['zlren']=$zlren;
            $list[$k]['num']=$num;
            $sumnum+=$num;
            $sumzlnum+=$zlnum;
            $sumzlren+=$zlren;
        }
       // dump($list);
        if(isset($_GET['hh'])){
            $type1='num';
            //dump($_GET);
            if($_GET['hh']=='desc'){
                $list = $this->my_sort($list,'num',SORT_DESC,SORT_STRING);
                $_SESSION['pqnum']="asc";
            }else{
                $list = $this->my_sort($list,'num',SORT_ASC,SORT_STRING);
                $_SESSION['pqnum']="desc";
            }
        }
        if(isset($_GET['zlcs'])){
        	$type1='zlnum';
        	//dump($_GET);
        	if($_GET['zlcs']=='desc'){
        		$list = $this->my_sort($list,'zlnum',SORT_DESC,SORT_STRING);
        		$_SESSION['zlcs']="asc";
        	}else{
        		$list = $this->my_sort($list,'zlnum',SORT_ASC,SORT_STRING);
        		$_SESSION['zlcs']="desc";
        	}
        }
        if(isset($_GET['zlrs'])){
        	$type1='zlren';
        	//dump($_GET);
        	if($_GET['zlrs']=='desc'){
        		$list = $this->my_sort($list,'zlren',SORT_DESC,SORT_STRING);
        		$_SESSION['zlrs']="asc";
        	}else{
        		$list = $this->my_sort($list,'zlren',SORT_ASC,SORT_STRING);
        		$_SESSION['zlrs']="desc";
        	}
        }
        /* dump($_SESSION);
        dump($list); */
        $this->assign('sumnum',$sumnum);
        $this->assign('sumzlnum',$sumzlnum);
        $this->assign('sumzlren',$sumzlren);
        $this->assign('list', $list);
        $this->assign('search', $name);
        $this->assign('page', $show);
        $this->display();
         
    }
    public function xiangqing(){
        //dump($_GET);
        $goods_id=I('goods_id');
        if($goods_id==''){
            $_SESSION['goods_id']=$_SESSION['goods_id'];
        }else{
            $_SESSION['goods_id']=$goods_id;
        }
        $goods_id=$_SESSION['goods_id'];
        $go=M('rob_goods_orders g');
        $list=$go->field("g.order_id,o.order_id,o.open_id,m.openid,o.add_time,g.goods_id,o.zhulirenshu,o.total_money,m.headimg,m.nickname,m.city,m.province,m.country")->join('yang_rob_orders o on g.order_id=o.order_id')->join('yang_member m on m.openid=o.open_id')->where(array('goods_id'=>$goods_id))->select();
        //dump($list);
        //此商品所有商品的订单
        foreach($list as $k=>$v){
        	$zhuliprice=M('goods')->field('goods_id,zhuli_price')->where(array('goods_id'=>$v['goods_id']))->find();
            //dump($zhuliprice);
        	$list[$k]['total_money']=(float)$zhuliprice['zhuli_price']*(float)$list[$k]['zhulirenshu'];
        }
        $count=$go->join('yang_rob_orders o on g.order_id=o.order_id')->join('yang_member m on m.openid=o.open_id')->where(array('goods_id'=>$goods_id))->count();
        $this->assign('count',$count);
        
        if(isset($_GET['pqje'])){
            $type1='pay_money';
            //dump($_GET);
            if($_GET['pqje']=='desc'){
                $list = $this->my_sort($list,'pay_money',SORT_DESC,SORT_STRING);
                $_SESSION['pqje']="asc";
            }else{
                $list = $this->my_sort($list,'pay_money',SORT_ASC,SORT_STRING);
                $_SESSION['pqje']="desc";
            }
        }
        if(isset($_GET['sf'])){
            $type1='total_money';
            //dump($_GET);
            if($_GET['sf']=='desc'){
                $list = $this->my_sort($list,'total_money',SORT_DESC,SORT_STRING);
                $_SESSION['sf']="asc";
            }else{
                $list = $this->my_sort($list,'total_money',SORT_ASC,SORT_STRING);
                $_SESSION['sf']="desc";
            }
        }
        if(isset($_GET['faqitime'])){
        	$type1='add_time';
        	//dump($_GET);
        	if($_GET['faqitime']=='desc'){
        		$list = $this->my_sort($list,'add_time',SORT_DESC,SORT_REGULAR);
        		$_SESSION['faqitime']="asc";
        	}else{
        		$list = $this->my_sort($list,'add_time',SORT_ASC,SORT_REGULAR);
        		$_SESSION['faqitime']="desc";
        	}
        }
        if(isset($_GET['zhuliren'])){
        	$type1='zhulirenshu';
        	//dump($_GET);
        	if($_GET['zhuliren']=='desc'){
        		$list = $this->my_sort($list,'zhulirenshu',SORT_DESC,SORT_STRING);
        		$_SESSION['zhuliren']="asc";
        	}else{
        		$list = $this->my_sort($list,'zhulirenshu',SORT_ASC,SORT_STRING);
        		$_SESSION['zhuliren']="desc";
        	}
        }
        $this->assign('list',$list);
        $this->display();
    }
    public function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){
        if(is_array($arrays)){
            foreach ($arrays as $array){
                if(is_array($array)){
                    $key_arrays[] = $array[$sort_key];
                }else{
                    return false;
                }
            }
        }else{
            return false;
        }
        array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
        return $arrays;
    }


}
    
